<?php

include('login_validation.php');
include('functions.php');
$bgcolor = "#229CBA";

$errors = array();

$selectedAuthors = array();

if (isset($_POST['insert']) && $_POST['insert'] == "insert") {

    $show = getValor("SHOW_ABSTRACT");

	$sessionId = getValor("ID_SESSION");

	$title = getValor('TITLE');

	if (isset($_POST['selectedAuthors'])) {
		$selectedAuthors = $_POST['selectedAuthors'];
	}

	$abstractText = $_POST['ABSTRACT_TEXT'];

	$abstractKeywords = $_POST['ABSTRACT_KEYWORDS'];

	$showProceeding = $_POST['SHOW_PROCEEDING'];

	$proceedingSearchKeys = $_POST['PROCEEDING_SEARCH_KEY'];


	if (strlen($title) == 0) {
		array_push($errors, "The title of the abstract cannot be empty");
	}
	

	
	if (sizeof($errors) == 0) {

		$idAbstract = getNextValSeq("abstract");
		
		$pdf = strtolower($_FILES["PROCEEDING_PDF_PATH"]["name"]);
		$extPdf = strtolower(substr($pdf,-3,3));
		$pdfFile = (date("dmYHis") + 1 ) . "." . $extPdf;
		$targetPdf = "proceedings/" . $pdfFile;
		move_uploaded_file($_FILES['PROCEEDING_PDF_PATH']['tmp_name'], $targetPdf);

		$insertSQL = sprintf("INSERT INTO ABSTRACT 
							(ID_ABSTRACT, ID_SESSION, SHOW_ABSTRACT, TITLE, ABSTRACT_TEXT, ABSTRACT_KEYWORDS, SHOW_PROCEEDING, 
								PROCEEDING_PDF_PATH, PROCEEDING_SEARCH_KEY) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($idAbstract, "int"),
					   GetSQLValueString($sessionId, "int"),
                       GetSQLValueString($show, "int"),
                       GetSQLValueString($title, "text"),
                       GetSQLValueString($abstractText, "text"),
                       GetSQLValueString($abstractKeywords, "text"),
                       GetSQLValueString($showProceeding, "int"),
                       GetSQLValueString($pdfFile, "text"),
                       GetSQLValueString($proceedingSearchKeys, "text"));
		
		executeUpdateQuery($insertSQL);
		
		foreach ($selectedAuthors as $selectedAuthor) {
			$insertAuthorSQL = "INSERT INTO ABSTRACT_AUTHOR (ID_AUTHOR, ID_ABSTRACT) VALUES (" . $selectedAuthor . ", " . $idAbstract . ")";
			executeUpdateQuery($insertAuthorSQL);
		}
		
		$success = "The abstract was successfully added!";
		
	}
}




?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

	<title>CONGRESS ADMIN - ADD ABSTRACT</title>

	<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
	<link href="accordion/style.css" rel="stylesheet" type="text/css" media="screen" />

	<script type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
	<script type="text/javascript" src="ajax_functions.js"></script>

	<script src="accordion/accordian.pack.js" language="javascript" type="text/javascript">
	</script>

	<style>
		body {
			padding: 0px;
			margin: 0px;
		}
		
		.bodybg {
			background: <?php echo $bgcolor; ?>
			filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='<?php echo $bgcolor; ?>', endColorstr='#FFFFFF');
			background: -webkit-gradient(linear, left top, left bottom, from(<?php echo $bgcolor; ?>), to(#FFFFFF));
			background: -moz-linear-gradient(top,  <?php echo $bgcolor ?>,  #FFFFFF);
			width:100%;
			height: 600px;
		}
	</style>

	<script type="text/javascript" src="jscolor/jscolor.js"></script>
	<script type="text/javascript" src="tiny_mice.js"></script>
	
	<script>
		function selectAuthors() {
			var authors = document.getElementById('selectedAuthors').options;
			var elements = authors.length;
			for (var i = 0; i < elements; i++) {
				authors[i].selected = true;
			}
		}
		
		function addAuthor() {
			var authors = document.getElementById('availableAuthors');
			var selectedAuthors = document.getElementById('selectedAuthors');

			var length = authors.options.length;
			for (var i = 0 ; i < length ; i++ ) {
				if (authors.options[i].selected) {
					var optsel = authors.options[i];
					var selectedAuthorsLength = selectedAuthors.length;
					selectedAuthors.options[selectedAuthorsLength] = optsel;
				}
			}
		}
		
		function removeAuthor() {
			var authors = document.getElementById('availableAuthors');
			var selectedAuthors = document.getElementById('selectedAuthors');

			var length = selectedAuthors.options.length;
			for (var i = 0 ; i < length ; i++ ) {
				if (selectedAuthors.options[i].selected) {
					var optsel = selectedAuthors.options[i];
					var availableAuthorsLength = authors.length;
					authors.options[availableAuthorsLength] = optsel;
				}
			}
		}
		
	</script>
</head>

<body onload="refreshSessionList(<?php echo getValor('ID_CONGRESS'); ?>, <?php if (getValor('ID_SESSION') != "") { echo getValor('ID_SESSION'); } else { echo "-1"; } ?>);">

	<div align="center" class="bodybg">

		<?php include('admin_header.php'); ?>

		<?php include('admin_menu.php'); ?>

		<?php include('congress_select.php'); ?>
		

	  <div id="basic-accordian" style="background-color: white;">

			<?php if (sizeof($errors) > 0) { include('errors.php'); } ?>
			
			<?php if (isset($success)) { include('success.php'); } ?>

			<?php if (getValor('ID_CONGRESS') != "") { ?>
            <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" onsubmit="selectAuthors();">

              <input type="hidden" name="insert" value="insert" />
              <input type="hidden" name="ID_CONGRESS" value="<?php echo getValor('ID_CONGRESS') ?>" />

              <table align="center" cellpadding="5">

				<tr>
					<td colspan="2">
						<div style="width: 650px; background-color: #DEE5E9; padding: 5px;" align="left">
							ABSTRACT SETTINGS
						</div>
					</td>
				</tr>

				<tr valign="baseline">
				  <td nowrap align="left">SHOW</td>
				  <td>
				  	<?php $isShowAbstract = getValor('SHOW_ABSTRACT'); ?>
				  	<input type="radio" value="1" name="SHOW_ABSTRACT" <?php if ($isShowAbstract == '' || $isShowAbstract == '1') { echo "checked='checked'"; } ?> 
					/> Yes
					<input type="radio" value="0" name="SHOW_ABSTRACT" <?php if ($isShowAbstract == '0') { echo "checked='checked'"; } ?> 
					/> No
				  </td>
				</tr>

				<tr valign="baseline">
				  <td nowrap align="left">SESSION</td>
				  <td>
				  	<div id="combosessiondiv">
					</div>
					<input type="text" name="newSession" id="newSession" maxlength="200" /> <input type="button" value="Add New Session" 
						onclick="addNewSession(<?php echo getValor('ID_CONGRESS') ?>, document.getElementById('newSession').value, 
									document.getElementById('ID_SESSION').value);"  />
				  </td>
				</tr>
				<tr valign="baseline">
				  <td nowrap align="left">TITLE</td>
				  <td><input type="text" name="TITLE" value="<?php echo getValor('TITLE') ?>" size="32" style="width:250px;"></td>
				</tr>
				
				<tr>
					<td colspan="2">
						<div style="width: 650px; background-color: #DEE5E9; padding: 5px;" align="left">
						   ABSTRACT AUTHORS
						</div>
					</td>
				</tr>
				
				<tr>
					<td colspan="2">
						<table width="100%" align="center" cellpadding="5">
							<tr>
								<td align="center">
									<select multiple="multiple" size="10" style="width:230px;" name="availableAuthors[]" id="availableAuthors" ondblclick="addAuthor();">
										<?php
											$authors = obtenerAuthorsPorCongressYNombre(getValor('ID_CONGRESS'), "", false);
										 	while ($author = mysql_fetch_array($authors)) {
												if (!in_array($author['ID_AUTHOR'], $selectedAuthors)) {
										?>
											<option value="<?php echo $author['ID_AUTHOR']; ?>"><?php echo $author['NAME']; ?></option>
										<?php 
												}
											} 
										?>
									</select>
								</td>
								<td align="center">
									<input type="button" value="&gt;" onclick="addAuthor();" /><br />
									<input type="button" value="&lt;" onclick="removeAuthor();" />
								</td>
								<td align="center">
									<select size="10" style="width: 230px;" name="selectedAuthors[]" id="selectedAuthors" multiple="multiple" 
										ondblclick="removeAuthor();">
										<?php
											$authors = obtenerAuthorsPorCongressYNombre(getValor('ID_CONGRESS'), "", false);
										 	while ($author = mysql_fetch_array($authors)) {
												if (in_array($author['ID_AUTHOR'], $selectedAuthors)) {
										?>
											<option value="<?php echo $author['ID_AUTHOR']; ?>"><?php echo $author['NAME']; ?></option>
										<?php 
												}
											} 
										?>
									</select>
								</td>
							</tr>
							<tr>
								<td colspan="3" align="right">
									<table>
										<tr>
											<td>Name</td>
											<td>Organization</td>
											<td>Country</td>
											<td>&nbsp;</td>
										</tr>
										<tr>
											<td><input type="text" name="author_name" id="author_name" /></td>
											<td><input type="text" name="author_organization" id="author_organization" /></td>
											<td><input type="text" name="author_country" id="author_country" /></td>
											<td>
												<input type="button" value="Add New Author" 
													onclick="addNewAuthor(<?php echo getValor('ID_CONGRESS') ?>, document.getElementById('author_name').value, 
																document.getElementById('author_organization').value,
																 document.getElementById('author_country').value)" />
											</td>
										</tr>
									</table>
									<div style="display:none;" id="updateAuthorListDiv">
									</div>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>

			<table align="center" cellpadding="5">				
				<tr>
					<td colspan="2">
						<div style="width: 650px; background-color: #DEE5E9; padding: 5px;" align="left">
							ABSTRACT TEXT
						</div>
					</td>
				</tr>
				<tr>
				  <td >
				  	<textarea name="ABSTRACT_TEXT"><?php echo getValor('ABSTRACT_TEXT'); ?></textarea>
				  </td>
				</tr>
			</table>
			<table align="center" cellpadding="5">
				<tr valign="baseline">
				  <td nowrap align="left">ABSTRACT KEYWORDS</td>
				  <td><input type="text" name="ABSTRACT_KEYWORDS" value="<?php echo getValor('ABSTRACT_KEYWORDS'); ?>" size="32"></td>
				</tr>
				
				<tr>
					<td colspan="2">
						<div style="width: 650px; background-color: #DEE5E9; padding: 5px;" align="left">
							PROCEEDING
						</div>
					</td>
				</tr>
				
				<tr valign="baseline">
				  <td nowrap align="left">SHOW PROCEEDING</td>
				  <td>
				  	<?php $isShowProceeding = getValor('SHOW_PROCEEDING'); ?>
				  	<input type="radio" value="1" name="SHOW_PROCEEDING" <?php if ($isShowProceeding == '' || $isShowProceeding == '1') { echo "checked='checked'"; } ?> 
					/> Yes
					<input type="radio" value="0" name="SHOW_PROCEEDING" <?php if ($isShowProceeding == '0') { echo "checked='checked'"; } ?> 
					/> No
				  </td>
				</tr>
				<tr valign="baseline">
				  <td nowrap align="left">INSERT PDF</td>
				  <td><input type="file" name="PROCEEDING_PDF_PATH" value="" size="32"></td>
				</tr>
				<tr valign="baseline">
				  <td nowrap align="left">SEARCH KEYWORDS</td>
				  <td><input type="text" name="PROCEEDING_SEARCH_KEY" value="<?php echo getValor('PROCEEDING_SEARCH_KEY'); ?>" size="32"></td>
				</tr>
				<tr>
					<td colspan="2">
						<div style="width: 650px; height:1px; background-color: #DEE5E9; padding: 0px;" align="left">
							&nbsp
						</div>
					</td>
				</tr>
				<tr valign="baseline">
				  <td nowrap align="left">&nbsp;</td>
				  <td align="right"><input type="submit" style="width:200px;" value="Save"></td>
				</tr>
			  </table>

            </form>
			<?php } ?>
            <p>&nbsp;</p>
	  </div>
		
	</div>

</body>
</html>
